home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / swingall.jar / javax / swing / tree / DefaultMutableTreeNode.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-07-15  |  9.1 KB  |  543 lines

  1. package javax.swing.tree;
  2.  
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.Serializable;
  7. import java.util.Enumeration;
  8. import java.util.NoSuchElementException;
  9. import java.util.Vector;
  10.  
  11. public class DefaultMutableTreeNode implements Cloneable, MutableTreeNode, Serializable {
  12.    public static final Enumeration EMPTY_ENUMERATION = new 1();
  13.    protected MutableTreeNode parent;
  14.    protected Vector children;
  15.    protected transient Object userObject;
  16.    protected boolean allowsChildren;
  17.  
  18.    public DefaultMutableTreeNode() {
  19.       this((Object)null);
  20.    }
  21.  
  22.    public DefaultMutableTreeNode(Object var1) {
  23.       this(var1, true);
  24.    }
  25.  
  26.    public DefaultMutableTreeNode(Object var1, boolean var2) {
  27.       this.parent = null;
  28.       this.allowsChildren = var2;
  29.       this.userObject = var1;
  30.    }
  31.  
  32.    public void add(MutableTreeNode var1) {
  33.       if (var1 != null && var1.getParent() == this) {
  34.          this.insert(var1, this.getChildCount() - 1);
  35.       } else {
  36.          this.insert(var1, this.getChildCount());
  37.       }
  38.  
  39.    }
  40.  
  41.    public Enumeration breadthFirstEnumeration() {
  42.       return new BreadthFirstEnumeration(this, this);
  43.    }
  44.  
  45.    public Enumeration children() {
  46.       return this.children == null ? EMPTY_ENUMERATION : this.children.elements();
  47.    }
  48.  
  49.    public Object clone() {
  50.       DefaultMutableTreeNode var1 = null;
  51.  
  52.       try {
  53.          var1 = (DefaultMutableTreeNode)super.clone();
  54.          var1.children = null;
  55.          var1.parent = null;
  56.          return var1;
  57.       } catch (CloneNotSupportedException var3) {
  58.          throw new InternalError(((Throwable)var3).toString());
  59.       }
  60.    }
  61.  
  62.    public Enumeration depthFirstEnumeration() {
  63.       return this.postorderEnumeration();
  64.    }
  65.  
  66.    public boolean getAllowsChildren() {
  67.       return this.allowsChildren;
  68.    }
  69.  
  70.    public TreeNode getChildAfter(TreeNode var1) {
  71.       if (var1 == null) {
  72.          throw new IllegalArgumentException("argument is null");
  73.       } else {
  74.          int var2 = this.getIndex(var1);
  75.          if (var2 == -1) {
  76.             throw new IllegalArgumentException("node is not a child");
  77.          } else {
  78.             return var2 < this.getChildCount() - 1 ? this.getChildAt(var2 + 1) : null;
  79.          }
  80.       }
  81.    }
  82.  
  83.    public TreeNode getChildAt(int var1) {
  84.       if (this.children == null) {
  85.          throw new ArrayIndexOutOfBoundsException("node has no children");
  86.       } else {
  87.          return (TreeNode)this.children.elementAt(var1);
  88.       }
  89.    }
  90.  
  91.    public TreeNode getChildBefore(TreeNode var1) {
  92.       if (var1 == null) {
  93.          throw new IllegalArgumentException("argument is null");
  94.       } else {
  95.          int var2 = this.getIndex(var1);
  96.          if (var2 == -1) {
  97.             throw new IllegalArgumentException("argument is not a child");
  98.          } else {
  99.             return var2 > 0 ? this.getChildAt(var2 - 1) : null;
  100.          }
  101.       }
  102.    }
  103.  
  104.    public int getChildCount() {
  105.       return this.children == null ? 0 : this.children.size();
  106.    }
  107.  
  108.    public int getDepth() {
  109.       Object var1 = null;
  110.  
  111.       for(Enumeration var2 = this.breadthFirstEnumeration(); var2.hasMoreElements(); var1 = var2.nextElement()) {
  112.       }
  113.  
  114.       if (var1 == null) {
  115.          throw new InternalError("nodes should be null");
  116.       } else {
  117.          return ((DefaultMutableTreeNode)var1).getLevel() - this.getLevel();
  118.       }
  119.    }
  120.  
  121.    public TreeNode getFirstChild() {
  122.       if (this.getChildCount() == 0) {
  123.          throw new NoSuchElementException("node has no children");
  124.       } else {
  125.          return this.getChildAt(0);
  126.       }
  127.    }
  128.  
  129.    public DefaultMutableTreeNode getFirstLeaf() {
  130.       DefaultMutableTreeNode var1;
  131.       for(var1 = this; !var1.isLeaf(); var1 = (DefaultMutableTreeNode)var1.getFirstChild()) {
  132.       }
  133.  
  134.       return var1;
  135.    }
  136.  
  137.    public int getIndex(TreeNode var1) {
  138.       if (var1 == null) {
  139.          throw new IllegalArgumentException("argument is null");
  140.       } else {
  141.          return !this.isNodeChild(var1) ? -1 : this.children.indexOf(var1);
  142.       }
  143.    }
  144.  
  145.    public TreeNode getLastChild() {
  146.       if (this.getChildCount() == 0) {
  147.          throw new NoSuchElementException("node has no children");
  148.       } else {
  149.          return this.getChildAt(this.getChildCount() - 1);
  150.       }
  151.    }
  152.  
  153.    public DefaultMutableTreeNode getLastLeaf() {
  154.       DefaultMutableTreeNode var1;
  155.       for(var1 = this; !var1.isLeaf(); var1 = (DefaultMutableTreeNode)var1.getLastChild()) {
  156.       }
  157.  
  158.       return var1;
  159.    }
  160.  
  161.    public int getLeafCount() {
  162.       int var1 = 0;
  163.       Enumeration var3 = this.breadthFirstEnumeration();
  164.  
  165.       while(var3.hasMoreElements()) {
  166.          TreeNode var2 = (TreeNode)var3.nextElement();
  167.          if (var2.isLeaf()) {
  168.             ++var1;
  169.          }
  170.       }
  171.  
  172.       if (var1 < 1) {
  173.          throw new InternalError("tree has zero leaves");
  174.       } else {
  175.          return var1;
  176.       }
  177.    }
  178.  
  179.    public int getLevel() {
  180.       int var2 = 0;
  181.  
  182.       for(Object var1 = this; (var1 = ((TreeNode)var1).getParent()) != null; ++var2) {
  183.       }
  184.  
  185.       return var2;
  186.    }
  187.  
  188.    public DefaultMutableTreeNode getNextLeaf() {
  189.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  190.       if (var2 == null) {
  191.          return null;
  192.       } else {
  193.          DefaultMutableTreeNode var1 = this.getNextSibling();
  194.          return var1 != null ? var1.getFirstLeaf() : var2.getNextLeaf();
  195.       }
  196.    }
  197.  
  198.    public DefaultMutableTreeNode getNextNode() {
  199.       if (this.getChildCount() != 0) {
  200.          return (DefaultMutableTreeNode)this.getChildAt(0);
  201.       } else {
  202.          DefaultMutableTreeNode var1 = this.getNextSibling();
  203.          if (var1 != null) {
  204.             return var1;
  205.          } else {
  206.             for(DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent(); var2 != null; var2 = (DefaultMutableTreeNode)var2.getParent()) {
  207.                var1 = var2.getNextSibling();
  208.                if (var1 != null) {
  209.                   return var1;
  210.                }
  211.             }
  212.  
  213.             return null;
  214.          }
  215.       }
  216.    }
  217.  
  218.    public DefaultMutableTreeNode getNextSibling() {
  219.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  220.       DefaultMutableTreeNode var1;
  221.       if (var2 == null) {
  222.          var1 = null;
  223.       } else {
  224.          var1 = (DefaultMutableTreeNode)var2.getChildAfter(this);
  225.       }
  226.  
  227.       if (var1 != null && !this.isNodeSibling(var1)) {
  228.          throw new InternalError("child of parent is not a sibling");
  229.       } else {
  230.          return var1;
  231.       }
  232.    }
  233.  
  234.    public TreeNode getParent() {
  235.       return this.parent;
  236.    }
  237.  
  238.    public TreeNode[] getPath() {
  239.       return this.getPathToRoot(this, 0);
  240.    }
  241.  
  242.    protected TreeNode[] getPathToRoot(TreeNode var1, int var2) {
  243.       TreeNode[] var3;
  244.       if (var1 == null) {
  245.          if (var2 == 0) {
  246.             return null;
  247.          }
  248.  
  249.          var3 = new TreeNode[var2];
  250.       } else {
  251.          ++var2;
  252.          var3 = this.getPathToRoot(var1.getParent(), var2);
  253.          var3[var3.length - var2] = var1;
  254.       }
  255.  
  256.       return var3;
  257.    }
  258.  
  259.    public DefaultMutableTreeNode getPreviousLeaf() {
  260.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  261.       if (var2 == null) {
  262.          return null;
  263.       } else {
  264.          DefaultMutableTreeNode var1 = this.getPreviousSibling();
  265.          return var1 != null ? var1.getLastLeaf() : var2.getPreviousLeaf();
  266.       }
  267.    }
  268.  
  269.    public DefaultMutableTreeNode getPreviousNode() {
  270.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  271.       if (var2 == null) {
  272.          return null;
  273.       } else {
  274.          DefaultMutableTreeNode var1 = this.getPreviousSibling();
  275.          if (var1 != null) {
  276.             return var1.getChildCount() == 0 ? var1 : var1.getLastLeaf();
  277.          } else {
  278.             return var2;
  279.          }
  280.       }
  281.    }
  282.  
  283.    public DefaultMutableTreeNode getPreviousSibling() {
  284.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  285.       DefaultMutableTreeNode var1;
  286.       if (var2 == null) {
  287.          var1 = null;
  288.       } else {
  289.          var1 = (DefaultMutableTreeNode)var2.getChildBefore(this);
  290.       }
  291.  
  292.       if (var1 != null && !this.isNodeSibling(var1)) {
  293.          throw new InternalError("child of parent is not a sibling");
  294.       } else {
  295.          return var1;
  296.       }
  297.    }
  298.  
  299.    public TreeNode getRoot() {
  300.       Object var1 = this;
  301.  
  302.       Object var2;
  303.       do {
  304.          var2 = var1;
  305.          var1 = ((TreeNode)var1).getParent();
  306.       } while(var1 != null);
  307.  
  308.       return (TreeNode)var2;
  309.    }
  310.  
  311.    public TreeNode getSharedAncestor(DefaultMutableTreeNode var1) {
  312.       if (var1 == this) {
  313.          return this;
  314.       } else if (var1 == null) {
  315.          return null;
  316.       } else {
  317.          int var2 = this.getLevel();
  318.          int var3 = var1.getLevel();
  319.          int var4;
  320.          Object var5;
  321.          Object var6;
  322.          if (var3 > var2) {
  323.             var4 = var3 - var2;
  324.             var5 = var1;
  325.             var6 = this;
  326.          } else {
  327.             var4 = var2 - var3;
  328.             var5 = this;
  329.             var6 = var1;
  330.          }
  331.  
  332.          while(var4 > 0) {
  333.             var5 = ((TreeNode)var5).getParent();
  334.             --var4;
  335.          }
  336.  
  337.          while(var5 != var6) {
  338.             var5 = ((TreeNode)var5).getParent();
  339.             var6 = ((TreeNode)var6).getParent();
  340.             if (var5 == null) {
  341.                if (var5 == null && var6 == null) {
  342.                   return null;
  343.                }
  344.  
  345.                throw new InternalError("nodes should be null");
  346.             }
  347.          }
  348.  
  349.          return (TreeNode)var5;
  350.       }
  351.    }
  352.  
  353.    public int getSiblingCount() {
  354.       TreeNode var1 = this.getParent();
  355.       return var1 == null ? 1 : var1.getChildCount();
  356.    }
  357.  
  358.    public Object getUserObject() {
  359.       return this.userObject;
  360.    }
  361.  
  362.    public Object[] getUserObjectPath() {
  363.       TreeNode[] var1 = this.getPath();
  364.       Object[] var2 = new Object[var1.length];
  365.  
  366.       for(int var3 = 0; var3 < var1.length; ++var3) {
  367.          var2[var3] = ((DefaultMutableTreeNode)var1[var3]).getUserObject();
  368.       }
  369.  
  370.       return var2;
  371.    }
  372.  
  373.    public void insert(MutableTreeNode var1, int var2) {
  374.       if (!this.allowsChildren) {
  375.          throw new IllegalStateException("node does not allow children");
  376.       } else if (var1 == null) {
  377.          throw new IllegalArgumentException("new child is null");
  378.       } else if (this.isNodeAncestor(var1)) {
  379.          throw new IllegalArgumentException("new child is an ancestor");
  380.       } else {
  381.          MutableTreeNode var3 = (MutableTreeNode)var1.getParent();
  382.          if (var3 != null) {
  383.             var3.remove(var1);
  384.          }
  385.  
  386.          var1.setParent(this);
  387.          if (this.children == null) {
  388.             this.children = new Vector();
  389.          }
  390.  
  391.          this.children.insertElementAt(var1, var2);
  392.       }
  393.    }
  394.  
  395.    public boolean isLeaf() {
  396.       return this.getChildCount() == 0;
  397.    }
  398.  
  399.    public boolean isNodeAncestor(TreeNode var1) {
  400.       if (var1 == null) {
  401.          return false;
  402.       } else {
  403.          Object var2 = this;
  404.  
  405.          while(var2 != var1) {
  406.             if ((var2 = ((TreeNode)var2).getParent()) == null) {
  407.                return false;
  408.             }
  409.          }
  410.  
  411.          return true;
  412.       }
  413.    }
  414.  
  415.    public boolean isNodeChild(TreeNode var1) {
  416.       boolean var2;
  417.       if (var1 == null) {
  418.          var2 = false;
  419.       } else if (this.getChildCount() == 0) {
  420.          var2 = false;
  421.       } else {
  422.          var2 = var1.getParent() == this;
  423.       }
  424.  
  425.       return var2;
  426.    }
  427.  
  428.    public boolean isNodeDescendant(DefaultMutableTreeNode var1) {
  429.       return var1 == null ? false : var1.isNodeAncestor(this);
  430.    }
  431.  
  432.    public boolean isNodeRelated(DefaultMutableTreeNode var1) {
  433.       return var1 != null && this.getRoot() == var1.getRoot();
  434.    }
  435.  
  436.    public boolean isNodeSibling(TreeNode var1) {
  437.       boolean var2;
  438.       if (var1 == null) {
  439.          var2 = false;
  440.       } else if (var1 == this) {
  441.          var2 = true;
  442.       } else {
  443.          TreeNode var3 = this.getParent();
  444.          var2 = var3 != null && var3 == var1.getParent();
  445.          if (var2 && !((DefaultMutableTreeNode)this.getParent()).isNodeChild(var1)) {
  446.             throw new InternalError("sibling has different parent");
  447.          }
  448.       }
  449.  
  450.       return var2;
  451.    }
  452.  
  453.    public boolean isRoot() {
  454.       return this.getParent() == null;
  455.    }
  456.  
  457.    public Enumeration pathFromAncestorEnumeration(TreeNode var1) {
  458.       return new PathBetweenNodesEnumeration(this, var1, this);
  459.    }
  460.  
  461.    public Enumeration postorderEnumeration() {
  462.       return new PostorderEnumeration(this, this);
  463.    }
  464.  
  465.    public Enumeration preorderEnumeration() {
  466.       return new PreorderEnumeration(this, this);
  467.    }
  468.  
  469.    private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
  470.       var1.defaultReadObject();
  471.       Object[] var2 = var1.readObject();
  472.       if (var2.length > 0 && var2[0].equals("userObject")) {
  473.          this.userObject = var2[1];
  474.       }
  475.  
  476.    }
  477.  
  478.    public void remove(int var1) {
  479.       MutableTreeNode var2 = (MutableTreeNode)this.getChildAt(var1);
  480.       this.children.removeElementAt(var1);
  481.       var2.setParent((MutableTreeNode)null);
  482.    }
  483.  
  484.    public void remove(MutableTreeNode var1) {
  485.       if (var1 == null) {
  486.          throw new IllegalArgumentException("argument is null");
  487.       } else if (!this.isNodeChild(var1)) {
  488.          throw new IllegalArgumentException("argument is not a child");
  489.       } else {
  490.          this.remove(this.getIndex(var1));
  491.       }
  492.    }
  493.  
  494.    public void removeAllChildren() {
  495.       for(int var1 = this.getChildCount() - 1; var1 >= 0; --var1) {
  496.          this.remove(var1);
  497.       }
  498.  
  499.    }
  500.  
  501.    public void removeFromParent() {
  502.       MutableTreeNode var1 = (MutableTreeNode)this.getParent();
  503.       if (var1 != null) {
  504.          var1.remove(this);
  505.       }
  506.  
  507.    }
  508.  
  509.    public void setAllowsChildren(boolean var1) {
  510.       if (var1 != this.allowsChildren) {
  511.          this.allowsChildren = var1;
  512.          if (!this.allowsChildren) {
  513.             this.removeAllChildren();
  514.          }
  515.       }
  516.  
  517.    }
  518.  
  519.    public void setParent(MutableTreeNode var1) {
  520.       this.parent = var1;
  521.    }
  522.  
  523.    public void setUserObject(Object var1) {
  524.       this.userObject = var1;
  525.    }
  526.  
  527.    public String toString() {
  528.       return this.userObject == null ? null : this.userObject.toString();
  529.    }
  530.  
  531.    private void writeObject(ObjectOutputStream var1) throws IOException {
  532.       var1.defaultWriteObject();
  533.       Object[] var2;
  534.       if (this.userObject != null && this.userObject instanceof Serializable) {
  535.          var2 = new Object[]{"userObject", this.userObject};
  536.       } else {
  537.          var2 = new Object[0];
  538.       }
  539.  
  540.       var1.writeObject(var2);
  541.    }
  542. }
  543.